package com.icss.daos;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.icss.beans.FileVO;
/**
 * 
 * @author 梁海成
 *处理文档中心的业务逻辑
 */
public class FileCenterDao {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
	private ResultSet rs=null;
	private Dao dao=new Dao();
	/**
	 * 关闭连接
	 *
	 */
	public void close(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		dao.close();
	}
	/**
	 * 获取用户文件
	 * 
	 * @param uid
	 * @param parentID
	 * @return
	 */
	public ArrayList<FileVO> getFiles(int uid,int parentID){
		String sql = "select * from Files where uid="+uid+" and parentID="+parentID+" and isFile=1";
		Dao dao = new Dao();
		ResultSet rs = dao.query(sql);
		ArrayList<FileVO> list = new ArrayList<FileVO>();
		try {
			while(rs.next()){
				FileVO file = new FileVO();
				file.setFileID(rs.getInt("fileID"));
				file.setFileName(rs.getString("filesName"));
				file.setFileURL(rs.getString("fileURL"));
				file.setIsFile(rs.getInt("isFile"));
				file.setParentID(rs.getInt("parentID"));
				file.setRemark(rs.getString("remark"));
				file.setIsShare(rs.getInt("isShare"));
				file.setUid(rs.getInt("uid"));
				file.setUploadTime(sdf.format(rs.getDate("uploadTime")));
				file.setChildID(rs.getString("childID"));
				file.setFileSize(rs.getInt("fileSize"));
				list.add(file);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dao.close();
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return list;
	}
	/**
	 * 获取用户文件目录
	 * @param uid
	 * @param parentID
	 * @return
	 */
	public ArrayList<FileVO> getFilesDIR(int uid,int parentID){
		String sql = "select * from Files where uid="+uid+" and parentID="+parentID+"and isFile=0";

		rs = dao.query(sql);
		ArrayList<FileVO> list = new ArrayList<FileVO>();
		try {
			while(rs.next()){
				FileVO file = new FileVO();
				file.setFileID(rs.getInt("fileID"));
				file.setFileName(rs.getString("filesName"));
				file.setFileURL(rs.getString("fileURL"));
				file.setIsFile(rs.getInt("isFile"));
				file.setParentID(rs.getInt("parentID"));
				file.setRemark(rs.getString("remark"));
				file.setIsShare(rs.getInt("isShare"));
				file.setUid(rs.getInt("uid"));
				file.setUploadTime(sdf.format(rs.getDate("uploadTime")));
				file.setChildID(rs.getString("childID"));
				list.add(file);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return list;
	}
	
	
	/**
	 * 获得用户根节点ID
	 * @param uid
	 * @return
	 */
	public int getDIRFileID(int uid,int pid){
		String dirsql = "select fileID from Files where uid="+uid+" and parentID="+pid;

		rs = dao.query(dirsql);
		int fileId =0;
		try {
			if(rs.next()){
				fileId = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return fileId;
		
	}
	
	/**
	 * 上传文件 
	 * @param file
	 * @return
	 */
	public boolean uploadFile(FileVO file){
		String sql = "insert into Files (uid,filesName,remark,isFile,fileURL,fileSize,parentID) values("+file.getUid()+",'"+file.getFileName()+"','"+file.getRemark()+"',"+file.getIsFile()+",'"+file.getFileURL()+"',"+file.getFileSize()+","+file.getParentID()+")";
		String PCsql ="select childID from Files where fileID="+file.getParentID();
		String str = "";
		Dao dao = new Dao();
		if(dao.execute(sql)){
			rs = dao.query(PCsql);
			
			try {
				if(rs.next()){
					str = rs.getString(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String getFileID = "select max(fileID) from Files";
			rs = dao.query(getFileID); 
			int insertFileID =0;
			try {
				if(rs.next()){
					insertFileID = rs.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String str2="";
			
			if(str==null){
				str2=insertFileID+",";
			}else{
				str2 = str+","+insertFileID;
			}
			String sql2 ="update Files set childID='"+str2+"' where fileID="+file.getParentID();
			
			if(dao.execute(sql2)){			
				return true;
			}else{
				return false;
			}
		}
		dao.close();
		close();
		return false;		
		
	}
	/**
	 * 新建文件目录
	 * @param file
	 * @param url
	 * @return
	 */
	
	public int CreateFileDIR(FileVO file,String  url){
		String sql = "insert into Files (uid,filesName,isFile,fileURL,parentID) values("+file.getUid()+",'"+file.getFileName()+"',0,'"+file.getFileURL()+"',"+file.getParentID()+")";
		String getFileID = "select max(fileID) from Files";
		dao.execute(sql);
		rs = dao.query(getFileID); 
		int insertFileID =0;
		try {
			if(rs.next()){
				insertFileID = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File fileDir = new File(url);
		
		fileDir.mkdir();
		close();
		return insertFileID;
	}
	
	/**
	 * 获取文件路径
	 * @param fileID
	 * @return
	 */
	public String GetFileURL(int fileID){
		String sql = "select fileURL from Files where fileID="+fileID;

		rs = dao.query(sql);
		String url="";
		try {
			if(rs.next()){
				url = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return url;
	}
	
	/**
	 * 获取该文件所在目录的名称
	 * @param fileID
	 * @return
	 */
	public String GetParentName(int fileID){
		String sql = "select filesName from Files where fileID="+fileID;
		Dao dao = new Dao();
		rs = dao.query(sql);
		String url="";
		try {
			if(rs.next()){
				url = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dao.close();
		close();
		return url;
	}
	
	/**
	 * 获取一个文件的信息
	 * @param fileID
	 * @return
	 */
	public FileVO GetFile(int fileID){
		String sql = "select f.*,u.userName from Files f inner join Users u on f.uid=u.uid where fileID ="+fileID;
		ResultSet rs = dao.query(sql);
		FileVO f = new FileVO();
		try {
			if(rs.next()){
				
				f.setFileID(rs.getInt("fileID"));
				f.setFileName(rs.getString("filesName"));
				f.setFileSize(rs.getInt("fileSize"));
				f.setFileURL(rs.getString("fileURL"));
				f.setRemark(rs.getString("remark"));
				f.setUid(rs.getInt("uid"));
				f.setUploadTime(sdf.format(rs.getDate("uploadTime")));
				f.setUserName(rs.getString("userName"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return f;
	}
	
	/**
	 * 获取目录下所有的文件
	 * @param fileID
	 * @return
	 */
	public ArrayList<FileVO> GetChildFiles(int fileID){
		String sql = "select f.*,u.userName from Files f inner join Users u on f.uid=u.uid where parentID ="+fileID;
		rs = dao.query(sql);
		ArrayList<FileVO> list = new ArrayList<FileVO>();
		try {
			while(rs.next()){
				FileVO f = new FileVO();
				f.setFileID(rs.getInt("fileID"));
				f.setFileName(rs.getString("filesName"));
				f.setFileSize(rs.getInt("fileSize"));
				f.setFileURL(rs.getString("fileURL"));
				f.setRemark(rs.getString("remark"));
				f.setUid(rs.getInt("uid"));
				f.setUploadTime(sdf.format(rs.getDate("uploadTime")));
				f.setUserName(rs.getString("userName"));
				list.add(f);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return list;
	}
	/**
	 * 转换文件路径
	 * @param str
	 * @return
	 */
	public String ChangeURL(String str){
		String[] url = str.split("\\\\");
		String result="";
		for(int i=0;i<url.length;i++){
			result = result+url[i]+"\\\\";
		}
		return result;
	}
	
	/**
	 * 判断文件是否共享
	 * @param fileID
	 * @return
	 */
	public int IsShare(int fileID){
		Dao dao =new Dao();
		String sql = "select isShare from Files where fileID ="+fileID;
		ResultSet rs = dao.query(sql);
		int isShare=0;
		try {
			if(rs.next()){
				isShare = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return isShare;
	}
	
	/**
	 * 共享文件
	 * @param fileID
	 * @return
	 */
	public boolean ShareFile(int fileID){
		String sql = "update Files set isShare=1 where fileID="+fileID;
		if(dao.execute(sql)){
			close();
			return true;
		}else{
			close();
			return false;
		}
	}
	
	/**
	 * 取消共享
	 * @param fileID
	 * @return
	 */
	public boolean UnShareFile(int fileID){
		String sql = "update Files set isShare=0 where fileID="+fileID;
		if(dao.execute(sql)){
			close();
			return true;
		}else{
			close();
			return false;
		}
	}

	/**
	 * 获取所有的共享文件
	 * @return
	 */
	public ArrayList<FileVO> getShareFilesDIR(){
		String sql = "select f.*,u.userName from Files f inner join Users u on f.uid=u.uid where isShare=1";
		rs = dao.query(sql);
		ArrayList<FileVO> list = new ArrayList<FileVO>();
		try {
			while(rs.next()){
				FileVO file = new FileVO();
				file.setFileID(rs.getInt("fileID"));
				file.setFileName(rs.getString("filesName"));
				file.setFileURL(rs.getString("fileURL"));
				file.setIsFile(rs.getInt("isFile"));
				file.setParentID(rs.getInt("parentID"));
				file.setRemark(rs.getString("remark"));
				file.setIsShare(rs.getInt("isShare"));
				file.setUid(rs.getInt("uid"));
				file.setUploadTime(sdf.format(rs.getDate("uploadTime")));
				file.setChildID(rs.getString("childID"));
				file.setUserName(rs.getString("userName"));
				list.add(file);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return list;
	}
	
	/**
	 * 更新文件
	 * @param vo
	 */
	public void updateFile(FileVO vo){
		String sql ="update Files set filesName='"+vo.getFileName()+"',remark='"+vo.getRemark()+"' where fileID="+vo.getFileID();
		Dao dao = new Dao();
		dao.execute(sql);
		dao.close();
		close();
	}
	
	/**
	 * 获取父节点的ID
	 * @param id
	 * @return
	 */
	public int GetParentID(int id){
		String sql ="select parentID from Files where fileID="+id;
		
		Dao dao = new Dao();
		int fileid=0;
		rs=dao.query(sql);
		try {
			if(rs.next()){
				fileid=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		dao.close();
		close();
		return fileid;
	}
	
	/**
	 * 修改文件 并将原文件删除 
	 * @param vo
	 * @param url
	 */
	public void updateFile2(FileVO vo,String url){
		String sql ="update Files set filesName='"+vo.getFileName()+"',remark='"+vo.getRemark()+"',fileURL='"+vo.getFileURL()+"' where fileID="+vo.getFileID();
		
		String str = "select fileURL from Files where fileID="+vo.getFileID();
		Dao dao = new Dao();
		rs=dao.query(str);
		String fileName = "";
		try {
			if(rs.next()){
				fileName = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		File file = new File(url+fileName);
		if(file.delete()){
			dao.execute(sql);
		}
		
		dao.close();
		close();
	}
	
	/**
	 * 删除文件
	 * @param id
	 * @param url
	 */
	public void delFile(int id,String url){
		String sql = "delete from Files where fileID="+id;
		String last="select fileURL from Files where fileID="+id;
		Dao dao1 =new Dao();
		rs = dao1.query(last);
		String fileName = "";
		try {
			if(rs.next()){
				fileName = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file = new File(url+fileName);
		if(file.delete()){
			Dao dao =new Dao();
			dao.execute(sql);
		}
		dao.close();
		dao1.close();
		close();
	}
	
	/**
	 * 批量删除文件
	 * @param str
	 * @param url
	 * @return
	 */
	public boolean delAllFile(String str,String url){
		String[] uidList = str.split(",");
		Dao dao =new Dao();
		for(int i=0;i<uidList.length;i++){
			String last="select fileURL from Files where fileID="+Integer.parseInt(uidList[i]);
			String sql = "delete from Files where fileID="+Integer.parseInt(uidList[i]);
			
			rs = dao.query(last);
			String fileName = "";
			try {
				if(rs.next()){
					fileName = rs.getString(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			File file = new File(url+fileName);
			if(file.delete()){
				
				dao.execute(sql);
			}		
			
		}
		dao.close();
		close();
		return true;
	}
	
	/**
	 * 删除共享文件夹
	 * @param fid
	 * @param url
	 */
	public void delShareFileDIR(int fid,String url){
		String sql1="select * from Files where parentID="+fid;
		String sql2="delete from Files where fileID="+fid;
		rs = dao.query(sql1);
		List<FileVO> fileList = new ArrayList<FileVO>();
		try {
			while(rs.next()){
				FileVO vo = new FileVO();
				vo.setFileID(rs.getInt("fileID"));
				vo.setFileURL(rs.getString("fileURL"));
				fileList.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(int i=0;i<fileList.size();i++){
			FileVO fvo = fileList.get(i);
			String sql = "delete from Files where fileID="+fvo.getFileID();
			File file = new File(url+fvo.getFileURL());
			file.delete();
			dao.execute(sql);
		}
		String sql3 = "select filesName from Files where fileID="+fid;
		rs= dao.query(sql3);
		String fileName="";
		try {
			if(rs.next()){
				fileName=rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(url+fileName);
		File file2 = new File(url+fileName);
		file2.delete();
		dao.execute(sql2);
		close();
	}

	/**
	 * 获取最新上传的文件
	 * @param uid
	 * @return
	 */
	public List<FileVO> getNewFiles(int uid){
		String sql ="select  * from Files where uid="+uid+" and isFile=1" + " limit 0,8";
		rs=dao.query(sql);
		List<FileVO> list = new ArrayList<FileVO>();
		try {
			while(rs.next()){
				FileVO vo = new FileVO();
				vo.setFileID(rs.getInt("fileID"));
				vo.setUid(rs.getInt("uid"));
				vo.setFileName(rs.getString("filesName"));
				vo.setUploadTime(sdf.format(rs.getDate("uploadTime")));
				list.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
